package com.mall.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mall.db.domain.Member;
import com.mall.db.service.MemberService;
import com.wxd.utils.StringUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/login/lc")
public class LoginController {


    @Resource(name="memberService")
    private MemberService ms;



    @RequestMapping("/toLogin")
    @ResponseBody
    public Map<String,Object> toLogin(@RequestParam(name = "loginName") String loginName,
                                      @RequestParam(name = "password") String password,
                                      @RequestParam(name = "code") String code,
                                      HttpServletRequest req, ModelMap mmap){
        Map<String,Object> map = new HashMap<>();

        //获取session
        HttpSession session = req.getSession();
        //获取验证码

        String imgCode = session.getAttribute("sessionCode").toString();


        //销毁session
        session.removeAttribute("sessionCode");

        if (StringUtils.isEmpty(code)||!imgCode.equals(code)){
            map.put("is",false);
            map.put("msg","验证码错误6");
            return map;
        }
        //



        if (StringUtils.isEmpty(loginName)){
            map.put("is",false);
            map.put("msg","登录账号不存在");
        }else {
            QueryWrapper<Member> qw = new QueryWrapper<>();
            qw.eq("login_Name",loginName);
            Member m = ms.getOne(qw);
            //验证码
            if (m.getPassword().equals(StringUtil.md5(password)) && !StringUtils.isEmpty(password)){
                map.put("is",true);
                map.put("msg","登陆成功");
                map.put("userName",m.getUserName());
                if ("1".equals(m.getRoleIds())){
                    map.put("url","/page/userIndex/UserIndex");
                }else if ("2".equals(m.getRoleIds())){
                    map.put("url","/page/system/ps");
                }
                m.setPassword("");
                session.setAttribute("member",m);//存入会话
                mmap.addAttribute("mmember",m);
            }else {
                map.put("is",false);
                map.put("msg","密码错误");
            }

        }

        return map;
    }

    @RequestMapping("/loginOut")
    @ResponseBody
    public Map<String,Object> loginout(HttpServletRequest req){
        HttpSession session = req.getSession();
        session.removeAttribute("member");//销毁session
        Map<String,Object> map = new HashMap<>();
        map.put("is",true);
        return map;
    }

    @RequestMapping("/code")
    @ResponseBody
    public Map<String,Object> getCode(@RequestParam(name = "code[]") List<String> code,
                          HttpSession session){

        String str = code.get(0)+""+code.get(1)+""+code.get(2)+""+code.get(3);

        session.setAttribute("sessionCode",str);

        return  null;
    }
}
